<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\home\model;
use think\Model;
use think\Db;
class PayModel extends Model
{

    /*
     * 获取session
     * */
    public function session()
    {
        $session=cmf_get_current_user();
        return $session;
    }
    /*
     * 支付
     * */
    public function pay($data,$where,$use_state)
    {
         $orderid=$data['order'];
         $state=DB::name('order')->where(['orderNo'=>$orderid])->find();
         if($state['payStatus']==0||$state['payStatus']==-1)
         {
             if($use_state==1)
             {
                 if($data['state']==1)
                 {
                     $finance=$this->finance($data,$where);
                     if($finance['sate']==1)
                         {
                             $order=$this->order($data,$where,$finance['time']);
                             $config=$this->setintegral($where['storeid']);
                             if($config['state']==1 && $config['deduction'] != 0){
                                 $this->gointegral($state,$config,$where['userid']);
                                 session('phoneone',null );
                             }
                             if($order==1) {
                                 $con['sate'] = 1;
                                 $con['con'] = '订单完成';
                                 return $con;
                             }
                         }else{
                             return $finance;
                         }
                 }elseif($data['state']==2){
                     $time=time();
                     $order=$this->order($data,$where,$time);
                     $config=$this->setintegral($where['storeid']);
                     if($config['state']==1 && $config['deduction'] != 0){
                         $this->gointegral($state,$config,$where['userid']);
                         session('phoneone',null );
                         if($order==1) {
                             $con['sate'] = 1;
                             $con['con'] = '订单完成';
                             return $con;
                         }
                     }
                 }
             }else{
                 if($data['state']==2)
                 {
                     $time=time();
                     $order=$this->order($data,$where,$time);
                     $con['sate'] = 1;
                     $con['con'] = '订单完成';
                     return $con;
                 }
             }
         }else{
            $con['sate']=0;
            $con['con']='订单已经支付';
            return $con;
         }

    }
    /*
     * 参数的传递
     * 要对现金进行判断（未做）
     * */
    public function order($data,$cause,$time)
    {
        if($cause==0)
        {
            $array['userid']=$cause['id'];
            $array['cardid']=$cause['cardid'];
        }else{
            $array['userid']=$cause['id'];
            $array['cardid']=$cause['cardid'];
        }
        $where['orderNo']=$data['order'];
        $array['actualMoney']=$data['s_pay'];
        $array['discount']=0;
        if($data['state']==1)
        {
            $array['getMoney']=0;
            $array['giveChange']=0;
        }elseif ($data['state']==2)
        {
            $array['getMoney']=$data['cash'];
            $array['giveChange']=$data['giveChange'];
        }

        $array['payType']=$data['state'];
        $array['payStatus']=1;
        $array['utime'] =$time;
        $state=DB::name('order')->where($where)->update($array);
        if($state)
        {
            $order_detail=$this->order_detail($data,$cause,$time);
            if($order_detail)
            {
                $con=1;
            }else{
                $con=0;
            }
            return $con;
        }else{
            $con=0;
            return $con;
        }
    }
    /*
     * 订单详情表
     * */
    public function order_detail($date,$cause,$time)
    {
       $where['orderid']=$date['order'];
       $data['paystatus']=1;
       $data['utime']=$time;
       $state=DB::name('order_detail')->where($where)->update($data);
       return $state;
    }
    /*
     * 余额表
     * */
    public function finance($data,$where)
    {
        $use=DB::name('finance')->where($where)->find();
        $new_oll=$use['money']-$data['s_pay'];
        $array['money']=$new_oll;
        $times=time();
        $state=DB::name('finance')->where($where)->update($array);
        if($state)
        {
            $time=$this->finance_record($data,$where,$times);
            if($time==1)
            {
                $con['sate']=1;
                $con['time']=$times;
                return $con;
            }else{
                $con['sate']=0;
                $con['time']=$times;
                return  $con;
            }
        }else{
            $con['sate']=0;
            $con['con']='不好意思现在余额支付不能使用';
            return  $con;
        }
    }
    /*
     * 余额流水表
     * */
    public function finance_record($date,$where,$time)
    {
        $data['userid']=$where['userid'];
        $data['orderid']=$date['order'];
        $data['money']=$date['s_pay'];
        $data['storeid']=$where['storeid'];
        $data['ctime']=$time;
        $data['operatorid']=1;
        $data['status']=1;
        $state= DB::name('finance_record')->insert($data);
        if($state)
        {
             return $sta=1;
        }else{
            return  $sta=0;
        }
    }
    /*
     * 购物赠送
     * */
    public function gointegral($state,$config,$id)
    {
        $integral=$this->user('integral',$id,$state['storeid']);
        $session=$this->session();
        $operatorid=$session['id'];
        $array['userid']=$id;
        $array['cardid']=$integral['cardid'];
        $array['storeid']=$state['storeid'];
        $array['operatorid']=$operatorid;
        $array['orderid']=$state['orderNo'];
        $moeny=floor($state['actualMoney']/$config['deductions']);
        $array['num']=$moeny;
        $array['ctime']=time();
        $sate=DB::name('integral_record')->insert($array);
        if($sate)
        {
            $where['userid']=$id;
            $where['storeid']=$state['storeid'];
            $new['num']=$integral['num']+$moeny;
            $data=DB::name('integral')->where($where)->update($new);

        }
    }
    /*
     * 抵扣积分表
     */
    public function integral($type,$useid,$storeid,$money,$oid,$integral=0)
    {
        if($type == 'xiaofei')
        {
            $where['userid']=$useid;
            $where['storeid']=$storeid;
            $num['num']=$money;
            $state=DB::name('integral')->where($where)->update($num);
            if($state)
            {
                $order=DB::name('order_detail')->where(['id'=>$oid])->find();
                $integral=$this->integral_record('xiaofei',$useid,$storeid,$money,$order,$integral);
            }
            return  $state;
        }

    }
    /*
     * 积分流水表
     * */
    public function integral_record($type,$useid,$storeid,$money,$order,$integrals)
    {

        if($type=='xiaofei')
        {
           $integral=$this->user('integral',$useid,$storeid);
           $session=$this->session();
           $operatorid=$session['id'];
           $array['userid']=$useid;
           $array['cardid']=$integral['cardid'];
           $array['storeid']=$storeid;
           $array['operatorid']=$operatorid;
           $array['orderid']=$order['orderid'];
           $array['num']=-$integrals;
           $array['ctime']=time();
           $sate=DB::name('integral_record')->insert($array);
           }

    }
    /*
     * 积分表的设置
     * */
    public function setintegral($shopid)
    {
        $where['storeid']=$shopid;
        $array=DB::name('config')->where($where)->order('id desc')->find();
        return $array;
    }
    /*
     * 查询会员的在本店铺的信息
     * */
    public  function user($table,$id,$storeid)
    {
        $where['userid']=$id;
        $where['storeid']=$storeid;
        $array=DB::name($table)->where($where)->order('id desc')->find();
        return $array;
    }

}